/**
 * 
 */
package com.abel.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.abel.domain.Application;
import com.abel.domain.Education;
import com.abel.domain.PersonSearchCriteria;
import com.abel.manager.EducationManager;
import com.abel.manager.exception.EducationException;
import com.abel.repository.EducationRepository;
import com.abel.repository.exception.RepositoryException;
import com.abel.util.UserSessionLocator;

/**
 * @author 
 *
 */
public class EducationManagerImpl implements EducationManager {


	
	/* (non-Javadoc)
	 * @see com.abel.manager.EducationManager#updateEducationInformation(com.abel.domain.Education)
	 */
	
    protected final Log logger = LogFactory.getLog(getClass());
    
    private EducationRepository educationRepository;

	@Override
	public void updateEducationInformation(Education education)
			throws EducationException {
		
		Application application = UserSessionLocator.getUserSession().getPerson().getApplication();
		
        if (education.getEduInfoId() == 0) {
            try {
                
				@SuppressWarnings("unused")
				int eduInfoId = this.educationRepository.addEducation(education);
				education.setEduInfoId(eduInfoId);
				
				//add education as a new list if no education exists in the list
				List<Education> list = null;
				if (application.getEducation() == null
						|| application.getEducation().size() == 0) {
					
					list = new ArrayList<Education>();
				}
				else {
					
					list = application.getEducation();
				}
				
				list.add(education);
				application.setEducation(list);
				
            }
            catch (RepositoryException e)
            {
                logger.debug("@EducationManagerImpl : exception " + e);
                e.printStackTrace();
                throw new EducationException("Exception caused while adding education information for " + education.getAplId(), e);
            }
     
        }
        else {
            try {
                this.educationRepository.updateEducation(education);
                
                //update existing education list assuming that the list exists
                List<Education> list = application.getEducation();
                if (list == null
                		|| list.size() == 0) {
                	throw new EducationException("Updating non existant education for user id " + UserSessionLocator.getUserSession().getPerson().getUserId());
                }
                //setting the updated education
                for (Education educationItem : list) {
                	if (education.getEduInfoId() == educationItem.getEduInfoId()) {
                		educationItem = education;
                		break;
                	}
                }
                
            }
            catch (RepositoryException e)
            {
                logger.debug("@EducationRepositoryImpl : exception " + e);
                e.printStackTrace();
                throw new EducationException("Exception caused while updating education information for " + education.getAplId(), e);
            }
     
        }

	}

	
	public void deleteEducationInformation(int aplId, int eduInfoId) throws EducationException
	{
		    try {
		        this.educationRepository.deleteEducation(aplId, eduInfoId);
		        
		      //getting education list
		        List<Education> list = UserSessionLocator.getUserSession().getPerson().getApplication().getEducation();
		        
		        //deleting the specific education from threadlocal list
		        for (Education educationItem : list) {
                	if (eduInfoId == educationItem.getEduInfoId()) {
                		list.remove(educationItem);
                		break;
                	}
                }
		    }
		    catch (RepositoryException e)
		    {
		        logger.debug("@EducationRepositoryImpl : exception " + e);
		        e.printStackTrace();
		        throw new EducationException("Exception caused while deleting education information for " + eduInfoId, e);
		    }
	}

	public void deleteEducationInformation(int eduInfoId) throws EducationException
	{
		    try {
		        this.educationRepository.deleteEducation(eduInfoId);
		        
		        //getting education list
		        List<Education> list = UserSessionLocator.getUserSession().getPerson().getApplication().getEducation();
                
		        //deleting the specific education from threadlocal list
		        for (Education educationItem : list) {
                	if (eduInfoId == educationItem.getEduInfoId()) {
                		list.remove(educationItem);
                		break;
                	}
                }
		    }
		    catch (RepositoryException e)
		    {
		        logger.debug("@EducationRepositoryImpl : exception " + e);
		        e.printStackTrace();
		        throw new EducationException("Exception caused while deleting education information for " + eduInfoId, e);
		    }
	}

	
	/* (non-Javadoc)
	 * @see com.abel.manager.EducationManager#getEducationInformation(com.abel.domain.PersonSearchCriteria)
	 */
	@Override
	public List<Education> getEducationInformation(PersonSearchCriteria criteria)
			throws EducationException {

		try {
			
			List<Education> list = educationRepository.getEducationInformation(criteria);
			
			//set education in threadlocal
			if (list.size() > 0) {
				UserSessionLocator.getUserSession().getPerson().getApplication().setEducation(list);
			}
			return list;
			
		} 
		catch (RepositoryException e) {
			e.printStackTrace();
			throw new EducationException("Exception while getting education information for " + criteria.getUserName(),e);
			
		}
	}

	@Override
	public void setEducationRepository(EducationRepository educationRepository) 
	{
		this.educationRepository = educationRepository;
	}

}
